package com.dogstalks.main;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.RectF;
import android.location.Location;
import android.util.Log;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;

public class MyTrackPositionOverlay extends Overlay{
	public final static String DEBUG = "dogstalks";
	private Context context;
    private final int mRadius = 5;
    
    
    private ArrayList<Location> mMyPath = new ArrayList<Location>();
    private ArrayList<MyPathLocation> mMyPath_P = new ArrayList<MyPathLocation>();
    private Location mBeforeLocation;
    private Location mCurrentLocation;
    
    private Location location;
    private Path mPath;
    private Paint mPaint;
    
    private int update = 0;
    
  
    public MyTrackPositionOverlay(Context context){
    	this.context = context;
    	mPath = new Path();
    	mPath.reset();
    	mPaint = new Paint();
    	mPaint.setAntiAlias(true);
    	mPaint.setDither(true);
    	mPaint.setColor(Color.BLUE);
    	mPaint.setStrokeWidth(3);
    	mPaint.setStyle(Paint.Style.FILL);
    }
    @Override
    public void draw(Canvas canvas, MapView mapView, boolean shadow) {
        Projection projection = mapView.getProjection();
        Log.d(DEBUG, "MyTrackPositionOverlay draw");
        for(int i=1; i<mMyPath.size(); i++)
        {
	        if(shadow == false && mMyPath.get(i) != null) {
	            //Double latitude = (mMyPath.get(i)).getLatitude()*1E6;
	            //Double longitude = (mMyPath.get(i)).getLongitude()*1E6;
	            //GeoPoint geoPoint;
	            //    geoPoint = new GeoPoint(latitude.intValue(), longitude.intValue());
	            
	            //Point point = new Point();
	            //projection.toPixels(geoPoint, point);
	            
	            //RectF oval = new RectF(point.x - mRadius, point.y - mRadius, point.x + mRadius, point.y + mRadius);
	            Path p = new Path();
	        	
	        	Point startPoint = new Point();
	            Point endPoint = new Point();
	            
	        	if(!(mMyPath.get(i-1)).equals("") && !(mMyPath.get(i-1)).equals(null))
	        	{
	        		projection.toPixels(new GeoPoint((int)((mMyPath.get(i-1).getLatitude()*1E6)),(int)((mMyPath.get(i-1).getLongitude()*1E6))), startPoint);
	        		projection.toPixels(new GeoPoint((int)((mMyPath.get(i).getLatitude()*1E6)),(int)((mMyPath.get(i).getLongitude()*1E6))), endPoint);
	        		
	        		p.moveTo(startPoint.x, startPoint.y);
	        		p.lineTo(endPoint.x, endPoint.y);
	        	}
	        	
	            Paint paint = new Paint();
	            //paint.setARGB(250, 255, 255, 255);
	            //paint.setColor(0x88FF0000);
	            //paint.setAntiAlias(true);
	            //paint.setFakeBoldText(true);
	            paint.setStrokeWidth(3);
	            paint.setStyle(Paint.Style.STROKE);
	            paint.setColor(Color.argb(255, 255, 0, 0));
	            
	            Paint backPaint = new Paint();
	            backPaint.setARGB(175, 50,50,50);
	            backPaint.setAntiAlias(true);
	            
	            //RectF backRect = new RectF(point.x + 2 + mRadius, point.y - 3*mRadius, point.x +65, point.y + mRadius);
	            
	            canvas.drawPath(p, paint);
	            //canvas.drawOval(oval, paint);
	            //canvas.drawRoundRect(backRect, 5, 5, backPaint);
	            //canvas.drawText("Location", point.x + 2*mRadius, point.y, paint);
	        }
	        super.draw(canvas, mapView, shadow);
	    }
    }
    
    @Override
    public boolean onTap(GeoPoint point, MapView mapView) {
    	mapView.removeAllViews();
    	update = 1;
        return false;
    }

    public Location getLocation() {
        return location;
    }
    
    public ArrayList<Location> getTrackingLocation(){
    	ArrayList<GeoPoint> point = new ArrayList<GeoPoint>();
    	return mMyPath;
    }
    
    public void setLocation(Location before, Location current) {
    	
    	if(before != null && current != null){
    		Log.d(DEBUG, "setLocation");
    		this.location = current;
    		
 	        //mMyPath.add(location);
    		mBeforeLocation = before;
    		mCurrentLocation = current;
    		mMyPath_P.add(new MyPathLocation(before, current));
    	
    	}
    	
       // Log.d(DEBUG, "위치 : "+location.getLatitude());
    }

    public void setLocation(Location location) {
    	if(location != null){
	        //this.location = location;
	        mMyPath.add(location);
    	}
      
    }
    
    class MyPathLocation{
    	public Location before;
    	public Location current;
    	public MyPathLocation(Location before, Location current){
    		this.before = before;
    		this.current = current;
    	}
    }
    
}


